/*
* Copyright (C) 2012 Manuel Pérez
*
* This file is part of OpenSudoku.
*
* OpenSudoku is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenSudoku is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenSudoku. If not, see <http://www.gnu.org/licenses/>.
*
*/
package igraf.prozezuak;

import igraf.ItzuliInterfazea;
import igraf.Panela;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import dBasea.Istripu;
import dBasea.Peritua;

public class PerituaEsleitu extends Panela {

	private static final long serialVersionUID = -4816253828115660663L;
	private JLabel zenbakia, hiriak;
	private JTextField nif;
	private JButton ok;
	private JButton ezeztatu;
	private JButton bilatu;
	private JComboBox bil;
	private Istripu istri = new Istripu();
	private Peritua perit = new Peritua();

	public PerituaEsleitu(String tituloa,
			final ItzuliInterfazea itzuliinterfazea) {
		super(tituloa);

		JPanel erdikoPanela = this.getErdikoPanela();
		erdikoPanela.setBackground(SystemColor.activeCaption);
		erdikoPanela.setLayout(new BorderLayout());

		// Erdiko panelak
		final JPanel erdikoa = new JPanel();
		erdikoa.setBackground(SystemColor.activeCaption);
		erdikoPanela.add(erdikoa, BorderLayout.CENTER);
		erdikoa.setLayout(new GridLayout(7, 1));

		JPanel g = new JPanel();
		g.setBackground(SystemColor.activeCaption);
		erdikoa.add(g);

		JPanel geg = new JPanel();
		geg.setBackground(SystemColor.activeCaption);
		erdikoa.add(geg);

		JPanel arri = new JPanel();
		arri.setBackground(SystemColor.activeCaption);
		FlowLayout fl_arri = new FlowLayout();
		fl_arri.setHgap(15);
		fl_arri.setVgap(10);
		arri.setLayout(fl_arri);
		zenbakia = new JLabel("Istripu zenbakia:");
		zenbakia.setFont(new Font("Arial", Font.PLAIN, 14));
		nif = new JTextField(8);
		arri.add(zenbakia);
		arri.add(nif);
		erdikoa.add(arri);

		bilatu = new JButton("Bilatu");
		bilatu.setIcon(new ImageIcon(PerituaEsleitu.class
				.getResource("/Argazkia/lupa5.png")));
		arri.add(bilatu);

		bil = new JComboBox();
		JPanel j = new JPanel();
		j.setBackground(SystemColor.activeCaption);
		FlowLayout farri = new FlowLayout();
		farri.setHgap(15);
		farri.setVgap(10);
		j.setLayout(farri);
		hiriak = new JLabel("Perituak hiri horretan:");
		hiriak.setFont(new Font("Arial", Font.PLAIN, 14));
		j.add(hiriak);
		j.add(bil);
		erdikoa.add(j);

		bilatu.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					// eremu hutsak zerbait idatzi bada
					if (!zerbaitIdatzi()) {
						JOptionPane.showMessageDialog(null,
								"Sartu behar duzu zerbait zuriunetan\n"
										+ "Ezin da eremu hutsik egon", "Oker",
								JOptionPane.ERROR_MESSAGE);
					}// existitzen bada istripua
					else if (!istri.dagoIstripua(nif.getText())) {
						JOptionPane.showMessageDialog(null,
								"Ez da istripu hori aurkitzen", "Oker",
								JOptionPane.ERROR_MESSAGE);
					} else {
						int istripu = Integer.parseInt(nif.getText());// istripua
																		// daukagu
						int hiria = istri.getHi(istripu);// hiria
															// del
															// istripu
															// anterior
						java.util.List<String> lista = perit
								.getPerituHiria(hiria);// lista bat tailer-hiria
						bil.removeAllItems();
						if (lista != null) {
							for (String hiriaa : lista) {
								bil.addItem(hiriaa);
							}
						}
						nif.setEnabled(false);
					}
				} catch (Exception e1) {
					e1.printStackTrace();
				}
			}

		});

		JPanel behekoa = new JPanel();
		FlowLayout flowLayout = (FlowLayout) behekoa.getLayout();
		flowLayout.setHgap(110);
		flowLayout.setVgap(10);
		behekoa.setBackground(SystemColor.activeCaption);
		ok = new JButton("Peritua esleitu istripu bati");
		ok.setHorizontalAlignment(SwingConstants.LEFT);
		ok.setFont(new Font("Arial", Font.PLAIN, 14));
		behekoa.add(ok);
		ezeztatu = new JButton("Ezeztatu");
		ezeztatu.setHorizontalAlignment(SwingConstants.RIGHT);
		ezeztatu.setFont(new Font("Arial", Font.PLAIN, 14));
		behekoa.add(ezeztatu);
		erdikoPanela.add(behekoa, BorderLayout.SOUTH);

		erdikoPanela.add(erdikoa, BorderLayout.CENTER);
		erdikoPanela.add(behekoa, BorderLayout.SOUTH);

		ok.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try {
					if (!zerbaitIdatzi()) {
						JOptionPane.showMessageDialog(null,
								"Sartu behar duzu zerbait zuriunetan\n"
										+ "Ezin da eremu hutsik egon", "Oker",
								JOptionPane.ERROR_MESSAGE);

					} else {

						Object hir = bil.getSelectedItem();
						String hu = hir.toString();

						perit.perituaEsleitu(Integer.parseInt(nif.getText()),
								hu);
						JOptionPane.showMessageDialog(null,
								" Peritua ondo esleitu da " + nif.getText()
										+ " istripuarekin", "",
								JOptionPane.INFORMATION_MESSAGE);
						eremuakGarbitu();
						nif.setEnabled(true);

					}

				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});

		ezeztatu.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				nif.setEnabled(true);
				ok.setEnabled(true);
				itzuliinterfazea.itzuliLehenPanela();
			}
		});

	}

	public void eremuakGarbitu() {
		nif.setText("");
		bil.removeAllItems();
	}

	private boolean zerbaitIdatzi() {
		boolean aurki;
		aurki = nif.getText().length() > 0;
		return aurki;
	}

}
